/*
 * Lec21.cpp
 *   Program to demonstrate function templates.
 *  Created on: Mar 1, 2014
 *      Author: Anthony Varghese - This is from the Savitch book
 */

#include <iostream>

using std::cout;
using std::endl;

/**
 * Interchanges the values of variable1 and variable2.
 * The assignment operator must work for the type T.
 */
template<class T>
void swapValues(T& variable1, T& variable2) {
      T temp;

      temp = variable1;
      variable1 = variable2;
      variable2 = temp;
}

/**
 * Precondition: numberUsed <= declared size of the array a.
 *  The array elements a[0] through a[numberUsed - 1] have values.
 *  The assignment and < operator work for values of type T.
 * Postcondition: The values of a[0] through a[numberUsed - 1] have
 *  been rearranged so that a[0] <= a[1] <=... <= a[numberUsed - 1].
 */
template<class T>
void sort(T a[], int numberUsed);

int main( ) {
    int integer1 = 1, integer2 = 2;
    cout << "Original integer values are "
         << integer1 << " " << integer2 << endl;
    swapValues(integer1, integer2);
    cout << "Swapped integer values are "
         << integer1 << " " << integer2 << endl;

    char symbol1 = 'A', symbol2 = 'B';
    cout << "Original character values are: "
         << symbol1 << " " << symbol2 << endl;
    swapValues(symbol1, symbol2);
    cout << "Swapped character values are: "
         << symbol1 << " " << symbol2 << endl;

    // ------------ Sorting using template function ------
    const int LISTSIZE = 4;
    int list[] = { 200, -11, 23, 1 };
    // Print unsorted array
    cout << "Unsorted array:  ";
    for (int i=0; i<LISTSIZE; i++)
    	cout << list[i] << " ";
    cout << endl;

    sort( list, LISTSIZE);

    // Print sorted array
    cout << "Sorted array:     ";
    for (int i=0; i<LISTSIZE; i++)
    	cout << list[i] << " ";
    cout << endl;
    return 0;
}

// The sort.cpp file contains the template definitions used above
// Note that in this case, the #include is at the end of the file and
//  it works! Makes you think what #include really does.
#include "sort.cpp"
